import json
import requests
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')


base_url = 'https://api.map.baidu.com/direction/v2/driving'
keys = ''
with open('config/key.txt', 'r') as p:
    keys = p.readlines()
    keys = keys[0].strip('\n')


class Route:
    def getRoute(start, end):
        data = {
            'origin': f'{start[1]},{start[0]}',
            'destination': f'{end[1]},{end[0]}',
            'tactics': 2,
            'origin_region': '成都',
            'destination_region': '成都',
            'ak': keys
        }
        res = requests.get(base_url, params=data)
        res = json.loads(res.text)
        location = []
        for i in res['result']['routes'][0]['steps']:
            path_route = i['path']
            path_route = path_route.split(';')
            for item in path_route:
                item_lng = item.split(',')[0]
                item_lat = item.split(',')[1]
                location.append([item_lng, item_lat])
        return location


# print(getRoute([104.02789, 30.6726], [104.01556, 30.65207]))
